package cn.herlioner.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.herlioner.dao.impl.Obj_TeaDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.dao.impl.TeacherDAOImpl;
import cn.herlioner.pojo.CRUObject;
import cn.herlioner.pojo.Obj_Tea;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.Teacher;
import cn.herlioner.service.CRUObjectService;
import cn.herlioner.service.PageService;
import freemarker.core.JSONOutputFormat;

public class ObjectAction extends ActionSupport implements SessionAware,ServletRequestAware{
	Obj_Tea obj_Tea;
	Obj_TeaDAOImpl obj_TeaDAO  = new Obj_TeaDAOImpl();
	List<Obj_Tea> obj_TeaDAOs = new ArrayList();
	
	Teacher teacher;
	TeacherDAOImpl teacherDAO = new TeacherDAOImpl();
	
	TSObject tSObject;
	ObjectDAOImpl objectDAO = new ObjectDAOImpl();
	List<TSObject> tSObjects = new ArrayList();
	
	CRUObject cruObject; 
	List<CRUObject> cruObjects = new ArrayList();
	CRUObjectService cruObjectService = new CRUObjectService();
	
	private int currentPagechange;//当前�?
	
	private PageService pageService;
	
	
	public int getCurrentPagechange() {
		return currentPagechange;
	}
	public void setCurrentPagechange(int currentPagechange) {
		this.currentPagechange = currentPagechange;
	}
	public PageService getPageService() {
		return pageService;
	}
	public void setPageService(PageService pageService) {
		this.pageService = pageService;
	}
	public TSObject gettSObject() {
		return tSObject;
	}
	public void settSObject(TSObject tSObject) {
		this.tSObject = tSObject;
	}
	public List<TSObject> gettSObjects() {
		return tSObjects;
	}
	public void settSObjects(List<TSObject> tSObjects) {
		this.tSObjects = tSObjects;
	}
	public Obj_Tea getObj_Tea() {
		return obj_Tea;
	}
	public void setObj_Tea(Obj_Tea obj_Tea) {
		this.obj_Tea = obj_Tea;
	}
	public Obj_TeaDAOImpl getObj_TeaDAO() {
		return obj_TeaDAO;
	}
	public void setObj_TeaDAO(Obj_TeaDAOImpl obj_TeaDAO) {
		this.obj_TeaDAO = obj_TeaDAO;
	}
	public List<Obj_Tea> getObj_TeaDAOs() {
		return obj_TeaDAOs;
	}
	public void setObj_TeaDAOs(List<Obj_Tea> obj_TeaDAOs) {
		this.obj_TeaDAOs = obj_TeaDAOs;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public TeacherDAOImpl getTeacherDAO() {
		return teacherDAO;
	}
	public void setTeacherDAO(TeacherDAOImpl teacherDAO) {
		this.teacherDAO = teacherDAO;
	}
	public TSObject getObject() {
		return tSObject;
	}
	public void setObject(TSObject tSObject) {
		this.tSObject = tSObject;
	}
	public ObjectDAOImpl getObjectDAO() {
		return objectDAO;
	}
	public void setObjectDAO(ObjectDAOImpl objectDAO) {
		this.objectDAO = objectDAO;
	}
	public List<TSObject> getObjects() {
		return tSObjects;
	}
	public void setObjects(List<TSObject> tSObjects) {
		this.tSObjects = tSObjects;
	}
	public CRUObject getCruObject() {
		return cruObject;
	}
	public void setCruObject(CRUObject cruObject) {
		this.cruObject = cruObject;
	}
	public List<CRUObject> getCruObjects() {
		return cruObjects;
	}
	public void setCruObjects(List<CRUObject> cruObjects) {
		this.cruObjects = cruObjects;
	}
	public CRUObjectService getCruObjectService() {
		return cruObjectService;
	}
	public void setCruObjectService(CRUObjectService cruObjectService) {
		this.cruObjectService = cruObjectService;
	}
	
	
	/**
	 * 获取组合的object列表
	 * @return
	 * @throws IOException 
	 * @throws Exception
	 */
	public String SelectObject() throws IOException{
		int pageSize = 7 ;
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		
		if(request.getParameter("currentPagechange")==null){
			currentPagechange = (int)session.get("currentPagechange");
			System.out.println(currentPagechange);
		}else{
			currentPagechange = Integer.valueOf(request.getParameter("currentPagechange"));
		}
		PageService Pageservice = objectDAO.PageSelect(currentPagechange, pageSize);
    	//获取组合表里面的组合信息
		try{
			for (int i = 0 ;i<pageSize;i++) {
		    	CRUObject cruObject = new CRUObject();
		    	TSObject obj = (TSObject)Pageservice.getRecordList().get(i);
		    	cruObject = cruObjectService.setobject(obj,cruObject);
		    	cruObjects.add(cruObject);}
			}catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				System.out.println("尾页不够pageSize了，但是不影响输出?");
			}
			//如果已经存在信息，则应该删除，然后更新信�?
		
		request.setAttribute("cruObjects", cruObjects);
    	request.setAttribute("PageService", Pageservice);
		return "success";
    }
	/**
	 * 删除课程
	 */
	public String delete(){
		//根据request域中的objnum值删除对应的课程
		Long objnum = cruObject.getObjnum();
		objectDAO.delete(objnum);
		HttpServletRequest request = ServletActionContext.getRequest();
		
		
		List<TSObject> TSObjects = objectDAO.list();
		for(TSObject tsObject : TSObjects) {
			CRUObject cruObject = new CRUObject();	
			cruObject = cruObjectService.setobjselct(tsObject.getObjnum(), cruObject);
			cruObjects.add(cruObject);
		}
		
		request.setAttribute("CRUObjects", cruObjects);
		return "success";
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	/**
	 * 修改某课
	 */
	public String update(){
		cruObjectService.update(cruObject);
		return "success";
	}
	
	public String getbyredi(){
		Map session = ActionContext.getContext().getSession();
		int redi = (int)session.get("redi");
		String objnameorteanameselect = (String) session.get("objnameorteanameselect");
		System.out.println(redi+objnameorteanameselect);
		
		int pageSize = 7 ;
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request.getParameter("currentPagechange")==null){
			currentPagechange = (int)session.get("currentPagechange");
			System.out.println(currentPagechange);
		}else{
			currentPagechange = Integer.valueOf(request.getParameter("currentPagechange"));
		}
		PageService Pageservice = objectDAO.PageSelect(currentPagechange, pageSize,redi);
		//获取组合表里面的组合信息
		try{
			for (int i = 0 ;i<pageSize;i++) {
		    	CRUObject cruObject = new CRUObject();
		    	TSObject obj = (TSObject)Pageservice.getRecordList().get(i);
		   		System.out.println("第"+i+"次信息");
		    	cruObject = cruObjectService.setobject(obj,cruObject);
		    	cruObjects.add(cruObject);
		    	}
			}catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				System.out.println("尾页不够pageSize了，但是不影响输出?");
			}
			//如果已经存在信息，则应该删除，然后更新信�?
		request.setAttribute("cruObjects", cruObjects);
    	request.setAttribute("PageService", Pageservice);
		
		return "getbyredisuccess";
	}
	
	//根据老师名称或者课程名称查询
	public String objnameorteanameselect(){
		Map session = ActionContext.getContext().getSession();
		String objnameorteanameselect = (String) session.get("objnameorteanameselect");
		System.out.println(objnameorteanameselect+"objnameorteanameselect");
		int pageSize = 7 ;
		PageService Pageservice = objectDAO.PageSelect(currentPagechange, pageSize,objnameorteanameselect);
    	//获取组合表里面的组合信息
		try{
			for (int i = 0 ;i<pageSize;i++) {
		    	CRUObject cruObject = new CRUObject();
		    	TSObject obj = (TSObject)Pageservice.getRecordList().get(i);
		   		System.out.println("第"+i+"次信息?");
		    	cruObject = cruObjectService.setobject(obj,cruObject);
		    	cruObjects.add(cruObject);
		    	}
			}catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				System.out.println("尾页不够pageSize了，但是不影响输出?");
			}
			//如果已经存在信息，则应该删除，然后更新信�?
		request.setAttribute("cruObjects", cruObjects);
    	request.setAttribute("PageService", Pageservice);
    	
		return "objnameorteanameselect";
	}
	
	@Override
	public void setSession(Map<String, java.lang.Object> arg0) {
		// TODO Auto-generated method stub
		
	}
	 private HttpServletRequest request;
	 public void setServletRequest(HttpServletRequest arg0) {
	        this.request = arg0;
	 }
	
}
